package org.jivesoftware.openfire.findpwd;

import java.io.UnsupportedEncodingException;
import java.util.Date;
import java.util.Properties;

import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

import org.jivesoftware.util.Blowfish;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class EmailUtils {
	private static final Logger log = LoggerFactory.getLogger(EmailUtils.class);
	 public static String myEmailAccount = "xinlizixunapp@sina.com";// 发件人电子邮箱
	  public static String myEmailPassword = "2017xinzixitong";
	  public static String myEmailSMTPHost = "smtp.sina.com.cn";   // 指定发送邮件的主机
	  public static String passwordKey = "F1kRWLdRAwGi8DL";
	  public static int port = 25;

	  public static String receiveMailAccount = "";   // 收件人电子邮箱
	 public static void sendEmail(FindPwdBean findpwdBean)
	   {   

	      // 获取系统属性
	      Properties properties = System.getProperties();
	 
	      // 设置邮件服务器
	      properties.setProperty("mail.smtp.host", myEmailSMTPHost);
	      properties.setProperty("mail.smtp.auth", "true");
	      properties.setProperty("mail.transport.protocol", "smtp");// 设置传输协议
	        properties.put("mail.debug", "true");//便于调试
	        properties.put("mail.smtp.port", port);
	      // 获取默认session对象
	      Authentication authentication = new Authentication(myEmailAccount, myEmailPassword); 
	      Session session = Session.getDefaultInstance(properties,authentication);
	
	 
	      try{
	         // 创建默认的 MimeMessage 对象
	         MimeMessage message = new MimeMessage(session);
	 
	         // Set From: 头部头字段
	         message.setFrom(new InternetAddress(myEmailAccount));
	         // Set To: 头部头字段
	         receiveMailAccount = findpwdBean.getEmail();
	         log.info(receiveMailAccount);
	         message.setRecipient(Message.RecipientType.TO, new InternetAddress(receiveMailAccount));
	       
	 
	         // 设置消息体
	         Blowfish bf = new Blowfish(passwordKey);  
		        String decryptString = bf.decrypt(findpwdBean.getEncryptedPassword());  
		        log.info(findpwdBean.getEncryptedPassword());
		        log.info(decryptString);
		        message.setContent("您使用了心咨系统的找回密码功能,您的用户信息为：用户名:"+findpwdBean.getUserName()+" ,密码:"+decryptString+" ,请您尽快修改密码！", "text/html;charset=utf-8");
		        // Set Subject: 头部头字段
		         message.setSubject("心咨找回密码服务");// 设置主题
		        message.setSentDate(new Date());;// 设置发信时间

	         // 发送消息
	         Transport.send(message);
	         log.info("Sent message successfully....");
	      }catch (MessagingException  mex) {
	         mex.printStackTrace();
	      }
	   }

}
class Authentication extends Authenticator{  
    String username=null;  
    String password=null;  

    public Authentication(){  
    }  
    public Authentication(String username, String password) {  
    this.username = username;  
    this.password = password;  
    }  
    protected PasswordAuthentication getPasswordAuthentication(){
    PasswordAuthentication pa = new PasswordAuthentication(username, password);
    return pa;
    }  
  }
